package com.sprouting.ops.controller;

import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.model.OpsApp;
import com.sprouting.ops.model.OpsAppBranch;
import com.sprouting.ops.model.qo.OpsAppBranchQo;
import com.sprouting.ops.service.OpsAppBranchService;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.qo.BaseQuery;
import com.sprouting.ops.service.OpsAppService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;

/**
 * (应用分支(版本)表)表控制层
 *
 * @author longx
 * @since 2023-11-20 10:53:14
 */
@Slf4j
@RestController
@RequestMapping("opsAppBranch")
public class OpsAppBranchController {

    @Autowired
    private OpsAppBranchService opsAppBranchService;
    @Autowired
    private OpsAppService opsAppService;

    /**
     * 逻辑删除
     * @param opsAppBranchId 主键
     * @return
     */
    @IdempotentSingle
    @PostMapping("delOpsAppBranch")
    public ResultJson delOpsAppBranch(String opsAppBranchId){
        if (CheckTool.checkNull(opsAppBranchId)){
            return ResultJson.errorCheck();
        }

        OpsAppBranch opsAppBranch = opsAppBranchService.getById(opsAppBranchId);
        if (opsAppBranch == null){
            return ResultJson.success();
        }

        // 逻辑删除
        opsAppBranchService.removeById(opsAppBranch);
        return ResultJson.success();
    }


    /**
     * 分页查询
     * @param query 查询参数
     * @return
     */
    @RequestMapping("getOpsAppBranchPageList")
    public ResultJson getOpsAppBranchPageList(@Validated OpsAppBranchQo query){
        Page page = query.getPage();
        return ResultJson.success(opsAppBranchService.getOpsAppBranchPageList(page, query));
    }

    /**
     * 保存
     * @param opsAppBranch 保存对象
     * @return
     */
    @IdempotentSingle
    @PostMapping("saveOpsAppBranch")
    public ResultJson saveOpsAppBranch(@Validated(AddGroup.class) OpsAppBranch opsAppBranch){
        String msg = checkOpsAppBranch(opsAppBranch);
        if (msg != null){
            return ResultJson.error(msg);
        }

        opsAppBranch.setId(IdTool.getId());
        EntityTool.saveBase(opsAppBranch);
        opsAppBranchService.save(opsAppBranch);
        return ResultJson.success(opsAppBranch);
    }

    /**
     * 更新
     * @param opsAppBranch 更新对象
     * @return
     */
    @IdempotentSingle
    @PostMapping("updOpsAppBranch")
    public ResultJson updOpsAppBranch(@Validated(UpdGroup.class) OpsAppBranch opsAppBranch){
        String msg = checkOpsAppBranch(opsAppBranch);
        if (msg != null){
            return ResultJson.error(msg);
        }

        EntityTool.updBase(opsAppBranch);
        opsAppBranchService.updateById(opsAppBranch);
        return ResultJson.success(opsAppBranch);
    }

    /**
     * 检查
     * @param opsAppBranch 校验对象
     * @return 非null则有异常
     */
    private String checkOpsAppBranch(OpsAppBranch opsAppBranch){
        OpsApp app = opsAppService.getById(opsAppBranch.getAppId());
        if (app == null){
            return "无效的应用";
        }
        OpsAppBranch dbBranch = opsAppBranchService.getByBranchNameFilterId(opsAppBranch.getBranchName(), opsAppBranch.getId(), opsAppBranch.getAppId());
        if (dbBranch != null){
            return "分支名称已经存在，请勿重复注册";
        }


        if (opsAppBranch.getStatus() == null){
            opsAppBranch.setStatus(ConstantBase.MaintenanceStatus.CONTINUOUS);
        }

        return null;
    }
}

